package online.shenme.day16;


import online.shenme.day09.demo.setTest.Student;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ClassLoaderTest {
    public static void main(String[] args) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException {
        Class<Class> classs = Class.class;
        Class<Student> studentClass = Student.class;
        //        method1();
        //        uesConstructor(classs, studentClass);
        useField(studentClass);
        useMethod(studentClass);
    }

    private static void useMethod(Class<Student> stu) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        for (Method method : stu.getMethods()) {
            System.out.println(method);
            method.invoke(stu.newInstance());
        }
    }

    private static void useField(Class<Student> stu) throws NoSuchFieldException {
        for (Field field : stu.getFields()) {
            System.out.println(field);
        }
    }


    //获取构造方法
    private static void uesConstructor(Class<Class> classs, Class<Student> studentClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Constructor<Student> constructor = studentClass.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);
        System.out.println(constructor.newInstance("123"));

        Constructor<Class> declaredConstructor = classs.getDeclaredConstructor(ClassLoader.class, Class.class);
        declaredConstructor.newInstance(ClassLoader.getSystemClassLoader(), studentClass);
    }

    //类的加载
    private static void method1() throws IOException {
        ClassLoader classLoader1 = ClassLoader.getSystemClassLoader();
        ClassLoader classLoader2 = classLoader1.getParent();
        ClassLoader classLoader3 = classLoader2.getParent();
        System.out.println(classLoader1);
        System.out.println(classLoader2);
        System.out.println(classLoader3);
        InputStream is = classLoader1.getResourceAsStream("prop.properties");
        Properties prop = new Properties();
        prop.load(is);
        System.out.println(prop);
    }
}
